package com.client.buttlefield.ui;

import java.io.IOException;
import java.io.InputStreamReader;

import javax.annotation.Resource;
import javax.jms.*;

public class MessageDispatcher {
	
	private static Connection connection;
	
	@Resource(mappedName="jms/ConnectionFactory")
	 private static ConnectionFactory connectionFactory;
	
	@Resource(mappedName="jms/Topic")
	 private static Topic topic;
	
	public static void main(String[] args) {
		Session mySess = null;
		
		try{
		connectionFactory  = new com.sun.messaging.ConnectionFactory();
		connection = connectionFactory.createConnection();
		mySess = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		topic = new com.sun.messaging.Topic("moving");
		
		MessageProducer myMsgProducer = mySess.createProducer(topic);
		MessageConsumer myMsgConsumer = mySess.createConsumer(topic);
		
		TextMessage myTextMsg = mySess.createTextMessage();
        myTextMsg.setText("Hello World");
        System.out.println("Sending Message: " + myTextMsg.getText());
        myMsgProducer.send(myTextMsg);
        
        connection.start();
        
        while(true){
        	Message msg = myMsgConsumer.receive();
			
			if(msg!= null){
		        if (msg instanceof TextMessage) {
		            TextMessage txtMsg = (TextMessage) msg;
		            System.out.println("Read Message: " + txtMsg.getText());
		        }
			}
		}
        

		
		} catch (Exception jmse) {
            System.out.println("Exception occurred : " + jmse.toString());
            jmse.printStackTrace();
        }finally{
        	try {
        		if(connection != null)
        				connection.close();
        		if(mySess != null)
        				mySess.close();
        		
			} catch (JMSException e) {
				e.printStackTrace();
			}
        }
	}
	
	public void send(){
		
	}
	
	
}
